/**
 * Copyright (c) 2007 Beijing Shiji Kunlun Software Co., Ltd. All Rights Reserved.
 * $Id: org.eclipse.jdt.ui.prefs 3459 2011-11-17 08:23:32Z andy.han $
 */
package org.credo.jws.failure;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import sun.misc.BASE64Decoder;

public class HttpSendPostWithMMS {

	public static void main(String[] args) {
		HttpSendPostWithMMS a = new HttpSendPostWithMMS();
		System.out.println("111");
		a.sendHttpPostRequest();

	}

	public boolean sendHttpPostRequest() {
		HttpClient httpClient = new DefaultHttpClient();
		int httpStatusCode = 400;
		boolean httpResult = false;
		try {
			System.out.println("gogogo");
			HttpPost httpPost = new HttpPost("http://mmsplat.eucp.b2m.cn/MMSCenterInterface/MMS.asmx/SendMMS");
			// ?userName=testDJ&password=pbpb123456&title=11&userNumbers=18612033529&MMSContent=acsadsdf&sendType=1

			// httpPost.setHeader("Content-Type",
			// "application/x-www-form-urlencoded");

			FileInputStream msgZipInputStream = new FileInputStream("D:\\restxml\\test2.zip");
			// byte[] Content = new byte[msgZipInputStream.available()];
			// msgZipInputStream.read(Content, 0, Content.length);
			// msgZipInputStream.close();
			// String strContent = new String(Content, "GBK");

			BASE64Decoder decoder = new BASE64Decoder();
			byte[] Content = decoder.decodeBuffer(msgZipInputStream);
			String strContent = new String(Content, "UTF-8");

			// 一个名称/值对参数用作一个元素的HTTP消息。保留以防需求变更
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("userName", "testDJ"));
			nvps.add(new BasicNameValuePair("password", "pbpb123456"));
			nvps.add(new BasicNameValuePair("title", "我我"));
			nvps.add(new BasicNameValuePair("userNumbers", "18612033529"));
			nvps.add(new BasicNameValuePair("MMSContent", strContent));
			// nvps.add(new BasicNameValuePair("MMSContent", strContent));
			nvps.add(new BasicNameValuePair("sendType", "1"));

			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, "GBK");

			httpPost.setEntity(entity);
			// HttpEntity fileEntity = new ByteArrayEntity(Content);
			// httpPost.setEntity(fileEntity);
			// String bi = "MMSContent=acsadsdf";
			// byte[] tempByte = objectConvertToByteArray(bi);
			// ByteArrayEntity tempByteEntity = new ByteArrayEntity(tempByte);
			// httpPost.setEntity(tempByteEntity);

			// StringBuffer out1 = new StringBuffer();
			// byte[] b1 = new byte[4096];
			// for (int n; (n = httpPost.getEntity().getContent().read(b1)) !=
			// -1;) {
			// out1.append(new String(b1, 0, n));
			// }
			// System.out.println("out1:" + out1.toString());

			HttpResponse response = httpClient.execute(httpPost);
			httpStatusCode = response.getStatusLine().getStatusCode();
			System.out.println("return的httpStatusCode为:" + httpStatusCode);
			//HttpEntity httpEntity = response.getEntity();
			//byte[] rtData = IOUtils.toByteArray(httpEntity.getContent());
			//System.out.println(new String(rtData));
			// if (httpEntity.isStreaming()) {
			// InputStream inputStream = httpEntity.getContent();
			// StringBuffer out = new StringBuffer();
			// byte[] b = new byte[4096];
			// for (int n; (n = inputStream.read(b)) != -1;) {
			// out.append(new String(b, 0, n));
			// }
			// System.out.println("inputStream:" + inputStream);
			// System.out.println("out:" + out.toString());
			// }
			if (httpStatusCode == 200) {
				httpResult = true;
			} else {
				httpResult = false;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return httpResult;
	}

	public static byte[] objectConvertToByteArray(Object obj) throws IOException {
		ObjectOutputStream os = null;

		ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
		os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
		os.flush();
		os.writeObject(obj);
		os.flush();
		byte[] sendBuf = byteStream.toByteArray();
		os.close();
		return sendBuf;
	}

}
